// 编辑个人资料
export function editProfile() {
    const urlParams = new URLSearchParams(window.location.search);
    const username = urlParams.get('username');
    window.location.href = 'edit-profile.html?username=' + username;
}

// 退出登录
export async function logout() {
    try {
        const response = await fetch('/logout', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        });
        const data = await response.json();
        if (data.message === '退出登录成功') {
            window.location.href = 'login.html';
        } else {
            console.error('退出登录失败:', data.message);
        }
    } catch (error) {
        console.error('网络错误:', error);
    }
}

export async function enterOnlinePractice(){
    const urlParams = new URLSearchParams(window.location.search);
    const username = urlParams.get('username');
    const response = await fetch(`/user-info?username=${username}`);
    const data = await response.json();
    if(data.role!=='教师'){
        alert('你不是教师，不能进入在线练习');
        return;
    }
    window.location.href='practice.html' + window.location.search;
}

export async function entertoDoPractice(){
    const urlParams = new URLSearchParams(window.location.search);
    const username = urlParams.get('username');
    const response = await fetch(`/user-info?username=${username}`);
    const data = await response.json();
    if(data.role=='教师'){
        alert('你是教师，不能做练习');
        return;
    }
    window.location.href='todo-practice.html' + window.location.search;
}

export async function openLearnTrack() {
    const urlParams = new URLSearchParams(window.location.search);
    const username = urlParams.get('username');
    const userResponse = await fetch(`/user-info?username=${username}`);
    const userData = await userResponse.json();

    if (userData.role !== '教师') {
        window.location.href = `learn-track.html?username=${username}`;
    } else {
        alert('教师无法使用该功能');
    }
}

export async function setupUserProfile(username) {
    try {
        // 1. 获取用户信息
        const response = await fetch(`/user-info?username=${username}`);
        if (!response.ok) throw new Error('获取用户信息失败');
        
        const userInfo = await response.json();
        
        // 2. 显示用户信息
        const userInfoContainer = document.getElementById('user-info-container');
        const userNameElement = document.getElementById('user-name');
        const studentIdElement = document.getElementById('student-id');
        
        userNameElement.textContent = userInfo.name || username;
        studentIdElement.textContent = userInfo.student_id || '学号未设置';
        userInfoContainer.classList.remove('hidden');
        
        // 3. 设置头像点击事件
        const userAvatar = document.getElementById('user-avatar');
        const dropdownMenu = document.getElementById('user-dropdown');
        
        userAvatar.addEventListener('click', function(e) {
            e.preventDefault();
            dropdownMenu.classList.toggle('hidden');
        });
        
        // 点击页面其他区域关闭下拉菜单
        document.addEventListener('click', function(e) {
            if (!userAvatar.contains(e.target) && !dropdownMenu.contains(e.target)) {
                dropdownMenu.classList.add('hidden');
            }
        });
    } catch (error) {
        console.error('设置用户信息失败:', error);
        // 错误处理：如果获取信息失败，至少显示用户名
        const userInfoContainer = document.getElementById('user-info-container');
        const userNameElement = document.getElementById('user-name');
        
        if (userInfoContainer && userNameElement) {
            userNameElement.textContent = username;
            userInfoContainer.classList.remove('hidden');
        }
    }
}
    